package org.uva.streambuilder;

import java.applet.Applet;
import java.awt.Color;
import java.util.Observable;


public class DerivativeStreamModule extends StreamModule {

	double lastval = 0;
	long lastTime = 0;
	
	public DerivativeStreamModule(Applet app, int x, int y) {
		super(app, x, y);
		this.defaultColor = new Color(26, 241, 133);
		this.selectColor = new Color(200, 20, 0, 200);
		this.name = "dx/dt";
		
	}
	
	
	public void update(Observable o, Object arg){
		if (arg instanceof Double)
		{
			double val = (Double) arg;
			
			Long currTime = System.currentTimeMillis();
			
			
			double deriv = (val - lastval)/(double)(currTime - lastTime);
			setValue(deriv);
			
			lastTime = System.currentTimeMillis();
			lastval = val;
		}
		
	};

}
